// const GET = 'GET';
const POST = 'POST';

// const baseURL = 'https://zyq.tjhaiqing.com/';//线上
// 
const baseURL = 'https://zyqtest.tjhaiqing.com/'; //测试


var RSA = require('./wx_rsa.js');
var encStr = "";
var publicKey = '-----BEGIN PUBLIC KEY-----' + 'MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAtrPSyhyxMCpFaA3cbtJTE0pgvGp35NJkI1zMudvXsFZa4rHdmKI09qvnQDAi5cNqaBsnbF++f3u9U6Wd3rh58OhEdE9xPA7rwKthnoL4gvr+IuzCoUoiijK2NkspxxkCoqpfFXzf3eBfNFhprLgIvrEq5vaugVp0e0gtjTXKSUR4Fq0qym90f2orzQ+d4WwIjyO010U8fEv7NsFavUVTl1jwuFDT+j9uEnnQOKur7v2GA2Tk0yd3RE0QSksDq5YOfSFjMQY3vK9EMNfwrF0KKFIaXRDs0O1oErwmI4F3Pg1FaiA68K7OyiP8nsKVcHfRBYVanZ/iJ0UprOP6xbpidi4+Ub39SuRkoeOYdERhHBXHZW9STNNSmhrgowUVafh+eT+vG6d3I2k79elk2J0fZKGnUAjQBtshxZ9ZYoI0hv7aN4ha/jc0H368NaNHFP+OOW12suxfQIqBew1MDaSk5cJEDOFnouldLgHNJXGnCxbuD/D72bEzcICNa7gR8TT/IABK85Y81HuHhhSu3zMMqWYvTLtu5gsXfZ62VeSDNCwqbpCfSKGd8aT+g08dOvgVMrjggbda7qx3VcEuFPIM3mgOQQMAGe9lbmnwg8Jfah/1RB+n4Qnt6dMDUkpwPx6tAgjTpFLjv48RMZBxt2oscERovKPyeoz2fxNt++O3tW8CAwEAAQ==' + '-----END PUBLIC KEY-----';
var encrypt_rsa = new RSA.RSAKey();
encrypt_rsa = RSA.KEYUTIL.getKey(publicKey);

function request(url, data) {
  // console.log(data)
  return new Promise(function (resolve, reject) {
    let header = {
      'content-type': 'application/x-www-form-urlencoded'
    };
    // var str = data;
    // console.log(str)
    // str=JSON.stringify(str)
    // console.log(str)
    // encStr = encrypt_rsa.encrypt(str)
    // console.log(encStr)
    // encStr = RSA.hex2b64(encStr);


    // encStr=encodeURIComponent(encStr);
    // console.log(encStr)
    // 暖艾生活馆和暖艾生活公用一套代码
    data.login_token = wx.getStorageSync('login_token')
    // console.log(data)
    wx.request({
      url: baseURL + url,
      method: 'POST',
      data: data,
      header: header,
      success(res) {
        //请求成功
        //判断状态码---状态根据后端定义来判断
        if (res.data.code == 0 || res.data.code == 2) {
          resolve(res);
        }
        // 当小程序为暖艾生活时
        // else if(url == 'shops/api.shop/get_shop_status_new')
        // else if(url == 'users/api.appPay/appPayFunc')
        // else if(url == 'users/api.wechat/getWxOpenInfo'||url == 'users/api.appPay/appPayFunc')
        // 需要显示以上信息  当小程序为暖艾生活馆时需要注掉
        else if (url == 'shops/api.shop/get_shop_status_new') {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
          wx.clearStorageSync();
          setTimeout(function () {
            wx.redirectTo({
              url: '/pages/login/pass/pass',
            })
          }, 1500)
        }
        else if (url == 'users/api.appPay/appPayFunc') {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
        }
        else if (url == 'users/api.wechat/getWxOpenInfo' || url == 'users/api.appPay/appPayFunc') {

        } else if (res.data.code == -1) {
          console.log(url, 8886)
          if (url != '/users/api.user/get_user_info') {

            wx.showToast({
              title: res.data.msg,
              icon: 'none',
              duration: 2000
            })
            wx.clearStorageSync();
            setTimeout(function () {
              wx.redirectTo({
                url: '/pages/login/pass/pass',
              })
              // 当小程序为暖艾生活时
              // wx.redirectTo({
              //   url: '/pages/login/phone/phone',
              // }) 信息需要注掉 当小程序为暖艾生活馆时显示
            }, 1500)
          } else {
            resolve(res);
          }

        } else {
          //其他异常
          reject(res);
          console.log(res)
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
        }
      },
      fail(err) {
        //请求失败
        reject(err)
        reject('运行时错误,请稍后再试');
        wx.showToast({
          title: '运行时错误,请稍后再试',
          icon: 'none',
          duration: 2000
        })
      }
    })
  })
}
function uploadImg(url) {
  return new Promise(function (resolve, reject) {
    let header = {
      'content-type': 'application/x-www-form-urlencoded'
    };
    wx.chooseImage({
      success(res) {
        const tempFilePaths = res.tempFilePaths
        console.log(tempFilePaths)

        wx.uploadFile({
          url: baseURL + url,
          filePath: tempFilePaths[0],
          name: 'file', //标识
          header: header,
          formData: { //其他所需要携带的参数，视情况而定
            'user': 'test'
          },
          success(res) {
            var dataJson = JSON.parse(res.data)
            //请求成功
            resolve(dataJson);
          },
          fail(err) {
            //请求失败
            var dataJson = JSON.parse(err.data)
            reject(dataJson)
            reject('运行时错误,请稍后再试');
            wx.showToast({
              title: '运行时错误,请稍后再试',
              icon: 'none',
              duration: 2000
            })
          }
        })



      },
      fail(res) {
        // console.log(res)
        reject(res)
      }
    })







  })
}
function uploadImgdt(url, data) {
  // data = true 多图
  return new Promise(function (resolve, reject) {
    let header = {
      'content-type': 'application/x-www-form-urlencoded'
    };
    let imglist = []
    wx.chooseImage({
      success(res) {
        const tempFilePaths = res.tempFilePaths
        console.log(tempFilePaths)

        tempFilePaths.forEach((item) => {
          wx.uploadFile({
            url: baseURL + url,
            filePath: data ? item : tempFilePaths[0],//,
            name: 'file', //标识
            header: header,
            formData: { //其他所需要携带的参数，视情况而定
              'user': 'test'
            },
            success(res) {
              if (data) {
                console.log(res, 11)
                var dataJson = JSON.parse(res.data)
                imglist.push(dataJson)
                //请求成功
                if (imglist.length == tempFilePaths.length) {
                  resolve(imglist);
                }

              } else {
                var dataJson = JSON.parse(res.data)
                //请求成功
                resolve(dataJson);
              }


            },
            fail(err) {
              //请求失败
              var dataJson = JSON.parse(err.data)
              reject(dataJson)
              reject('运行时错误,请稍后再试');
              wx.showToast({
                title: '运行时错误,请稍后再试',
                icon: 'none',
                duration: 2000
              })
            }
          })
        })
      },
      fail(res) {
        // console.log(res)
        reject(res)
      }
    })
  })
}
const API = {
  getOpenid: (data) => request('users/api.wechat/getWxOpenInfo', data), //获取用户openid
  phoneLogin: (data) => request('users/api.user/login_by_sms_code', data), //手机验证码登录
  registeruser: (data) => request('users/api.user/register_user', data), //注册用户
  sendSmsCode: (data) => request('users/api.user/send_sms_code', data), //发送短信验证码
  loginByPassword: (data) => request('users/api.user/login_by_password', data), //密码登录
  getloginconfig: (data) => request('users/api.user/get_login_config', data), //获取登陆配置
  getShopStatus: (data) => request('shops/api.shop/get_shop_status', data), //获取用户店铺申请状态
  getShopStatusNew: (data) => request('shops/api.shop/get_shop_status_new', data), //获取用户店铺申请状态-新
  getShopRules: (data) => request('shops/api.shop/get_shop_rules', data), //获取店铺规则
  getArea: (data) => request('shops/api.shop/get_area', data), //获取所有地址
  getUserAddressList: (data) => request('shops/api.shop/get_user_address_list', data),//获取地址
  uploadImages: (data) => uploadImg('index/api.upload/file', data), //上传图片
  uploadImagesdt: (data) => uploadImgdt('index/api.upload/file', data), //上传图片
  getGoods: (data) => request('orders/api.orders/get_goods', data), //获取二次配货产品
  getptGoods: (data) => request('/orders/api.orders/get_pt_goods', data), //获取普通产品列表
  getGoodshuodong: (data) => request('orders/api.orders/get_goods_huodong', data), //获取活动产品列表
  editUserAddress: (data) => request('shops/api.shop/edit_user_address', data),// 用户添加 / 修改收货地址
  delUserAddress: (data) => request('shops/api.shop/del_user_address', data), //  删除用户收货地址
  comfirmOrder: (data) => request('orders/api.orders/confirm_order', data), // 订单下单--配货专区购买下单
  comfirmOrdernew: (data) => request('orders/api.orders/confirm_order_new', data), // 订单下单--配货专区购买下单 -- 新
  applyShop: (data) => request('shops/api.shop/apply_shop', data), // 申请店铺 / 待支付状态下修改店铺
  applyShopUpgrade: (data) => request('shops/api.shop/apply_shop_upgrade', data), // 升级店铺
  wxPay: (data) => request('users/api.wechat/wxPay', data), //支付接口
  getRegisterGoods: (data) => request('shops/api.shop/get_register_goods_new', data), //首次配货
  getApplyShopDetail: (data) => request('shops/api.shop/get_apply_shop_detail', data), //查看申请店铺详情
  editAvatar: (data) => request('users/api.user/edit_avatar', data), //修改头像
  // editShopApply: (data) => request('shops/api.shop/edit_shop_apply', data), //店铺数据修改 - 只修改店铺申请数据
  editShopApply: (data) => request('shops/api.shop/edit_shop_apply_new', data), //店铺数据修改 - 只修改店铺申请数据
  cancleShopInfo: (data) => request('shops/api.shop/cancle_shop_info', data), //申请店铺取消/申请店铺的订单取消
  cancleUpgrade: (data) => request('shops/api.shop/cancle_upgrade', data), //取消升级店铺
  getOrderDetail: (data) => request('orders/api.orders/get_order_detail', data), //获取订单详情
  editOrderAddress: (data) => request('orders/api.orders/edit_order_address', data), //修改待支付订单地址
  getExpressDelivery: (data) => request('orders/api.orders/get_express_delivery', data), //获取订单物流信息
  confirmDelivery: (data) => request('orders/api.orders/confirm_delivery', data), //确认收货
  getOrderLists: (data) => request('orders/api.orders/get_order_lists', data), //获取用户订单列表
  shopCheckLists: (data) => request('shops/api.check/shop_check_lists', data), //获取待审核(全部)店铺列表
  delShopImage: (data) => request('shops/api.shop/del_shop_image', data), // 删除店铺图片
  updateShopImage: (data) => request('shops/api.shop/update_shop_image', data), //更新店铺图片
  checkShop: (data) => request('shops/api.check/check_shop', data), //审核店铺申请
  getSlides: (data) => request('shops/api.shop/get_slides', data), //获取二次配货的轮播图 
  delOrder: (data) => request('orders/api.orders/del_order', data), //二次购物订单取消
  refundCheck: (data) => request('shops/api.check/refund_check', data), //退款审核
  fahuo: (data) => request('shops/api.check/fahuo', data), //发货
  getAdminManageProvices: (data) => request('shops/api.check/get_admin_manage_provices', data), //获取初审人员可以管辖的省份
  updateShopLicense: (data) => request('shops/api.shop/update_shop_license', data), //更新营业执照相关信息
  getApplyDefaultData: (data) => request('shops/api.shop/get_apply_default_data', data), //未申请店铺前：获取店铺之前的数据
  getAsaleLists: (data) => request('orders/api.asale/get_asale_lists', data), //获取退款售后列表
  selectOrdersGoods: (data) => request('orders/api.asale/select_orders_goods', data), //选择需要售后商品
  getAsaleReason: (data) => request('orders/api.asale/get_asale_reason', data), //查询退换货原因
  submitAsale: (data) => request('orders/api.asale/submit_asale', data), //提交售后
  getAsaleDetail: (data) => request('orders/api.asale/get_asale_detail', data), //查看售后详情
  submitAsaleLogistics: (data) => request('orders/api.asale/submit_asale_logistics', data), //提交物流信息
  getAsaleLogistics: (data) => request('orders/api.asale/get_asale_logistics', data), //查看物流信息
  cancleAsale: (data) => request('orders/api.asale/cancle_asale', data), //撤销售后
  getOrderNum: (data) => request('orders/api.orders/get_order_num', data), //获取用户订单数量
  getCompanyAndPhone: (data) => request('orders/api.asale/get_company_and_phone', data), //获取快递公司和用户默认手机号
  getDcStatus: (data) => request('shops/api.shop/get_dc_status', data), //用户是否可以购买大舱产品
  getDcGoods: (data) => request('shops/api.shop/get_dc_goods', data), //获取大舱产品列表
  getGoodsProducts: (data) => request('shops/api.shop/get_goods_products', data), //获取产品详情数据
  myjf: (data) => request('orders/api.jf/myjf', data), //获取我的积分和积分收支列表数据
  productList: (data) => request('orders/api.jf/product_list', data), //获取积分换购产品列表
  confirmJforder: (data) => request('orders/api.jf/confirm_jforder', data), //积分换购
  getShopConfig: (data) => request('shops/api.shop/get_shop_config', data), //获取分批付款的数据
  dealFpCancle: (data) => request('users/api.wechat/dealFpCancle', data), //获取分批付款的数据
  shopcheckin: (data) => request('/shops/api.shop/shop_checkin', data), //礼包订单登记
  getgoodsproductsnew: (data) => request('/shops/api.shop/get_goods_products_new', data), //商品详情
  getptgoodsproductsnew: (data) => request('/shops/api.shop/get_pt_goods_products_new', data), //获取普通产品详情
  getupgradedefaultnum: (data) => request('/shops/api.shop/get_upgrade_default_num', data), //升级前获取礼包购买区间
  submitFeedback: (data) => request('/users/api.user/submit_feedback', data), //升级前获取礼包购买区间
  getuserinfo: (data) => request('/users/api.user/get_user_info', data), //获取用户信息
  getOpenidAppPay: (data) => request('users/api.appPay/getWxOpenInfo', data), //获取用户openid
  appPayFunc: (data) => request('users/api.appPay/appPayFunc', data), //支付接口-除微信小程序外的支付接口
  underlinePay: (data) => request('orders/api.orders/underline_pay', data), //二次配货线下支付
  getpaymethodnew: (data) => request('orders/api.orders/get_pay_method_new', data), //获取支付方式-新
  getpaymethodfinal: (data) => request('orders/api.orders/get_pay_method_final', data), //获取支付方式-新-最终
  getbanks: (data) => request('users/api.appPay/get_banks', data), //获取支付方式-新-最终
  addUserBank: (data) => request('users/api.appPay/addUserBank', data), //绑定银行卡---第一步:传入银行卡等用户信息(点击获取验证码
  bankbankremsg: (data) => request('users/api.appPay/bank_bank_remsg', data), //绑定银行卡---第一步--重发短信
  confirmUserBank: (data) => request('users/api.appPay/confirmUserBank', data), //绑定银行卡---第二步:确认银行卡绑定状态(绑定)
  bankpayconfirm: (data) => request('users/api.appPay/bank_pay_confirm', data), //银行卡支付确认短信验证码
  bankpayremsg: (data) => request('users/api.appPay/bank_pay_remsg', data), //银行卡支付--重发短信
  getmyconfig: (data) => request('users/api.user/get_my_config', data), //获取我的-功能显示配置
  getmeetinglists: (data) => request('users/api.webInfo/get_meeting_lists', data), //获取邀请会议列表
  getshareusers: (data) => request('users/api.webInfo/get_share_users', data), //获取邀请人详情列表
  getmeetinginfo: (data) => request('users/api.webInfo/get_meeting_info', data), //获取会议对应的二维码链接等信息
  gethuodongconfig: (data) => request('shops/api.shop/get_huodong_config', data), //获取活动开关配置
  smauth: (data) => request('/users/api.appPay/sm_auth', data), //非店铺用户实名认证
  editavatar: (data) => request('/users/api.user/edit_password', data), //修改密码
  bindinvite: (data) => request('/users/api.user/bind_invite', data), //绑定优惠码
  edituserinfo: (data) => request('/users/api.user/edit_user_info', data), //用户修改昵称和性别
  editorderfukimgs: (data) => request('/orders/api.orders/edit_order_fukimgs', data), //修改订单付款凭证
  getgwzdapplylists: (data) => request('/users/api.gwzd/get_gwzd_apply_lists', data), //获取顾问驻店申请列表
  applygwzdinfo: (data) => request('/users/api.gwzd/apply_gwzdinfo', data), //提交顾问驻店申请
  applyappraise: (data) => request('/users/api.gwzd/apply_appraise', data), //提交顾问驻店评价
  getferightfee: (data) => request('/orders/api.orders/get_feright_fee', data), //获取运费
  getAllVideo: (data) => request('/users/api.studyZone/getAllVideo', data), //获取全部播放的视频
  getPlayUrl: (data) => request('/users/api.studyZone/getPlayUrl', data), //获取视频播放地址
  getAliveInfo: (data) => request('/lives/api.lives/get_alive_info', data), //获取当前开播的直播间信息
  getaccesstoken: (data) => request('/lives/api.lives/get_access_token', data), //获取长连接token
  getptgoodszq: (data) => request('/orders/api.orders/get_pt_goods_zq', data), //获取普通产品列表-专区
  getPosts: (data) => request('/users/api.syssetting/getPosts', data), //获取海报
  getBanners: (data) => request('/pindex/api.pindex/get_banners', data), //获取banner数据
  getBannersdata: (data) => request('/pindex/api.pindex/get_banners_data', data), //获取banner详情数据
  wsshopinfo: (data) => request('/shops/api.shop/ws_shop_info', data), // 完善店铺资料
  getindexzq: (data) => request('/pindex/api.pindex/get_index_zq', data), // 获取专区
  getindexzqdata: (data) => request('/pindex/api.pindex/get_index_zq_data', data), // 获取专区数据
  getlivegoods: (data) => request('/lives/api.lives/get_live_goods', data), // 获取直播商品列表
  getlivefudai: (data) => request('/lives/api.lives/get_live_fudai', data), // 获取直播福袋信息
  livesign: (data) => request('/lives/api.lives/live_sign', data), // 直播签到
  fudaiaward: (data) => request('/lives/api.lives/fudai_award', data), // 福袋抽奖-发布弹幕时调用
  getuserfudaiaward: (data) => request('/lives/api.lives/get_user_fudai_award', data), // 查询用户是否中奖
  liveconfirmorder: (data) => request('/orders/api.orders/live_confirm_order', data), // 直播订单下单
  getproductsdetail: (data) => request('/shops/api.shop/get_product_detail', data), //获取产品详情
  addshoppingcart: (data) => request('/orders/api.orders/add_shopping_cart', data), //加入购物车
  getshoppingcart: (data) => request('/orders/api.orders/get_shopping_cart', data), //获取购物车列表
  deleteshoppingcart: (data) => request('/orders/api.orders/delete_shopping_cart  ', data), //删除购物车
  getcargoodnum: (data) => request('/orders/api.orders/get_car_good_num', data), //获取购物车数量
  getfudaiawardlists: (data) => request('/lives/api.lives/get_fudai_award_lists', data), //获取福袋中奖列表
  getmyconfignew: (data) => request('/users/api.user/get_my_config_new', data), // 获取我的-功能显示配置
  
  


};
module.exports = {
  API: API
}
